package com.huifer.wgx.controller;

import com.huifer.wgx.model.VerifyEntity;
import com.huifer.wgx.pojo.CommonResult;
import com.huifer.wgx.service.VerifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title : VerifyController </p>
 * <p>Description : 校验内容</p>
 *
 * @author huifer
 * @date 2019-03-15
 */
@RestController
@RequestMapping("verify")
@Api(tags = "VerifyController", description = "校验项管理")
public class VerifyController {

    @Autowired
    private VerifyService verifyService;
    private CommonResult commonResult = new CommonResult();

    @ApiOperation("创建校验规则")
    @PostMapping("/save")
    public ResponseEntity saveVerify(
            @RequestParam("verify_name") String verifyName,
            @RequestParam("verify_type") String verifyType
    ) {
        try {
            VerifyEntity verifyEntity = verifyService.saveVerify(verifyName, verifyType);
            return ResponseEntity.ok(commonResult.success(verifyEntity));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

    @ApiOperation("查看所有校验规则")
    @GetMapping("/find_all")
    public ResponseEntity findAllWork(@RequestParam("pagenum") Integer pageNum,
            @RequestParam("pagesize") Integer pageSize) {
        try {
            return ResponseEntity
                    .ok(commonResult.success(verifyService.findAll(pageNum, pageSize)));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(commonResult.fialed(e));
        }
    }

}
